package com.mingda.weathermagiccenter.task;


import com.mingda.weathermagiccenter.dto.StationBean;
import com.mingda.weathermagiccenter.dto.StationFcBean;
import com.mingda.weathermagiccenter.dto.WeatherFcResultBean;
import com.mingda.weathermagiccenter.dto.WeatherResultBean;
import com.mingda.weathermagiccenter.pojo.WsStationData;
import com.mingda.weathermagiccenter.pojo.WsStationFcData;
import com.mingda.weathermagiccenter.service.WsStationDataService;
import com.mingda.weathermagiccenter.service.WsStationFcDataService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.ResponseEntity;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.client.RestTemplate;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Async
@Slf4j
@Configuration
@EnableScheduling // 定时刷新物化视图
@ConditionalOnProperty(prefix = "weatherapi",name = "enabled",havingValue = "true")
public class refreshTask {


    @Autowired
    private JdbcTemplate jdbcTemplate;

    /**
     * 刷新物化视图 每个小时刷新
     */
    @Scheduled(cron = "0 0/60 * * * ?")
    public void refreshMv() {
        try {
            jdbcTemplate.execute("REFRESH MATERIALIZED VIEW CONCURRENTLY ws_fx_norainday");
    // 记录日志
            System.out.println("[" + java.time.LocalDateTime.now() + "] Refreshed ws_fx_norainday CONCURRENTLY");
        } catch (Exception e) {
    // 生产环境建议改成日志记录/告警
            System.err.println("MV refresh failed: " + e.getMessage());
        }
    }





}
